package com.pengpeng.server.dao;


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.pengpeng.server.database.Database;
import com.pengpeng.server.database.DatabaseFactory;
import com.pengpeng.server.database.ISqlObj;
import com.pengpeng.server.entity.Server;
import com.pengpeng.server.entity.User;

public class ServerDao{

	private DatabaseFactory databasefactory;
	
	public Server queryByServername(String servername)
	{
		RowMapper<Server> mapper=new RowMapper<Server>()
		{
			@Override
			public Server mapRow(ResultSet rs, int arg1) throws SQLException {
				String servername="";
				String serverurl="";
				
				
				servername=rs.getString("servername");
				serverurl=rs.getString("serverurl");
				
				
				Server server=new Server();
				server.setServername(servername);
				server.setServerurl(serverurl);
				
				return server;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryServerByServername");
		final Object[] params = new Object[]{servername};
	    try {  
	    	  Server server=jdbctemplate.queryForObject(sqlstr,params, mapper);
			  return  server;
	        } catch (EmptyResultDataAccessException e) {  
	          return null;  
	    } 
	}//queryByServername
	
	public List<Server> query()
	{
		RowMapper<Server> mapper=new RowMapper<Server>()
		{
			@Override
			public Server mapRow(ResultSet rs, int arg1) throws SQLException {
				String servername="";
				String serverurl="";
				
				
				servername=rs.getString("servername");
				serverurl=rs.getString("serverurl");
				
				
				Server server=new Server();
				server.setServername(servername);
				server.setServerurl(serverurl);
				
				return server;
			}//mapRow
		};//new RowMapper
		
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("queryServer");
	    try {  
	    			  
	    	List<Server> serverlist=jdbctemplate.query(sqlstr, mapper);
			  return  serverlist;
	        } catch (EmptyResultDataAccessException e) {  
	          return null;  
	    } 
	}//queryByServername
	
	public boolean update(Server server)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();	
		
		Object[] params = new Object[] {server.getServername(),server.getServerurl(),server.getServername()};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("updateServer");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}
	
	public boolean insert(Server server)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		
		String servername=server.getServername();
		String serverurl=server.getServerurl();
			
		Object[] params = new Object[] {servername,serverurl};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("insertServer");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}
	public boolean delete(Server server)
	{
		Database database=databasefactory.getDatabase("default");
		String sqlstr="";
		JdbcTemplate jdbctemplate=database.getJdbcTemplate();
		Object[] params = new Object[] {server.getServername()};
		ISqlObj sqlobj=database.getSqlobj();
		sqlstr=sqlobj.getSql("deleteServer");
		jdbctemplate.update(sqlstr,params);		
		return true;
	}//delete
	public DatabaseFactory getDatabasefactory() {
		return databasefactory;
	}
	public void setDatabasefactory(DatabaseFactory databasefactory) {
		this.databasefactory = databasefactory;
	}

}
